package com.sky.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端-地址薄接口")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;


    @PostMapping
    @ApiOperation("新增地址薄")
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("新增地址薄：{}", addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());//设置用户id
        addressBook.setIsDefault(0);//默认不是默认地址
        addressBookService.save(addressBook);
        return Result.success();


    }


    @GetMapping("/list")
    @ApiOperation("查询登录用户所有地址")
    public Result listAddressBook() {
        log.info("查询登录用户所有地址");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        return Result.success(addressBookService.list(wrapper));
    }


    @GetMapping("/default")
    public Result defaultAddressBook() {
        log.info("查询登录用户默认地址");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault, 1);
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        //默认地址最多只有一个，所以用getOne方法查询
        return Result.success(addressBookService.getOne(wrapper));

    }

    @PutMapping
    @ApiOperation("修改地址薄")
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("根据Id修改地址：{}", addressBook);


        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getId, addressBook.getId());
        wrapper.set(addressBook.getConsignee() != null, AddressBook::getConsignee, addressBook.getConsignee())
                .set(addressBook.getPhone() != null, AddressBook::getPhone, addressBook.getPhone())
                .set(addressBook.getSex() != null, AddressBook::getSex, addressBook.getSex())
                .set(addressBook.getDetail() != null, AddressBook::getDetail, addressBook.getDetail())
                .set(addressBook.getIsDefault() != null, AddressBook::getIsDefault, addressBook.getIsDefault())
                .set(addressBook.getLabel() != null, AddressBook::getLabel, addressBook.getLabel());
        addressBookService.update(wrapper);

        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据Id查询地址薄")
    public Result getAddressBook(@PathVariable Long id) {
        log.info("根据Id查询地址薄：{}", id);
        return Result.success(addressBookService.getById(id));
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址薄")
    public Result deleteAddressBook(@RequestParam Long id) {
        log.info("根据Id删除地址薄：{}", id);
        addressBookService.removeById(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址：{}", addressBook);
        //1.将当前用户所有的地址改成非默认地址
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault, 0)
                .eq(AddressBook::getUserId, BaseContext.getCurrentId());
        addressBookService.update(wrapper);

        //2.将当前地址设置为默认
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success();

    }

}
